//
//  CZVistorView.swift
//  crkWeibo
//
//  Created by 陈荣科 on 15/12/16.
//  Copyright © 2015年 crk. All rights reserved.
//

import UIKit

//1、懒加载所有控件
//2、添加控件
//3、设置位置

//注册和登录按钮点击时间协议
protocol CZVistorViewDelegate: NSObjectProtocol {

    //协议里面默认是必须实现的
    //注册按钮点击事件
    func visitorViewRegisterClick()
    
    //登录按钮点击事件
    func visitorViewLoginClick()

}


class CZVistorView: UIView {

    //代理属性  可能有可能没有
    weak var delegate: CZVistorViewDelegate?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件，设置frame
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    //添加控件
    func prepareUI(){
        
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        //添加子控件
        addSubview(iconView)
        
        //遮罩层
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLable)
        addSubview(registerButton)
        addSubview(login)
        
        //关闭Autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLable.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        login.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        
        //使用AutoLayout,添加约束
        
        ///给iconView添加约束
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))

        
        ///给homeView添加约束

        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
       
        self.addConstraint( NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        ///给messageLable添加约束
        self.addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 300))
        
        self.addConstraint( NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //给注册按钮添加约束
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        
        //给登录按钮添加约束
        self.addConstraint(NSLayoutConstraint(item: login, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: login, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    
        self.addConstraint(NSLayoutConstraint(item: login, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        self.addConstraint(NSLayoutConstraint(item: login, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //给遮罩层添加约束
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))

        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
       self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
      self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -200))
    }
    
    

    //给其他访客试图设置图片和lable的值
    func setupVisitorInfo(imagName: String, message: String){

        //图片
        iconView.image = UIImage(named: imagName)
        
        //label
        messageLable.text = message
        
        //隐藏小房子
        homeView.hidden = true
        
        //遮罩放到最低部
        self.sendSubviewToBack(coverView)
    }
    
    
    // 转轮旋转的动画
    func startRotationAnimation() {
        // 核心动画, 旋转
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        // 设置参数
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        // 动画完成时不移除
        anim.removedOnCompletion = false
        
        // 给图层添加核心动画
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复动画
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        // 重置偏移时间
        iconView.layer.timeOffset = 0
        
        // 重置开始时间
        iconView.layer.beginTime = 0
        
        // 计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        // 设置开始时间
        iconView.layer.beginTime = timeSincePause
    }
    
    
    
    @objc private func registerClick() {
        // 调用代理通知控制器按钮被点击
        // delegate有值就调用visitorViewRegisterClick
        // delegate没有值就不执行visitorViewRegisterClick
        delegate?.visitorViewRegisterClick()
    }
    
    func loginClick() {
        // view是不能modal出来控制器, 只有控制器才能modal出来
        // 点击事件需要传给控制器
        
        delegate?.visitorViewLoginClick()
    }

    
    ///懒加载
    
    
    ///遮罩层
    private lazy var coverView: UIImageView = {
    
        let imageView = UIImageView()
        
        imageView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return imageView
    }()
    
    ///转轮
    private lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    ///小房子
    private lazy var homeView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    ///消息
    private lazy var messageLable: UILabel = {
    
        let label = UILabel()
        
        //设置文字
        label.text = "关注一些人,看看这里有什么惊喜!"
        label.textColor = UIColor.grayColor()
        
        label.numberOfLines = 0
        
        label.textAlignment = NSTextAlignment.Center
        
        return label
        
    }()
    
    //注册
    private lazy var registerButton: UIButton = {
    
        let button = UIButton()
        
        //设置按钮背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //设置文字
        button.setTitle("注册", forState: UIControlState.Normal)
        
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        
    }()
    
    //登录按钮
    private lazy var login: UIButton = {
    
        let button = UIButton()
        
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        
        //设置文字
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        
        //设置文字颜色
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()

}
